//
//  PreTransactionPage.swift
//  Share
//
//  Created by admin on 2020/12/7.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI

struct PreTransactionPage: View {
    
    @ObservedObject var store =  PreTransactionPageStore()
    @State var showSheet = false
    var sheetStort: Store?
    
    var body: some View {
        NavigationView{
            VStack{
                List{
                    Section(header: Text("买入")){
                        
                        ForEach(store.instores) { item in
                            HStack{
                                ShareView(share: item.share)
                                Spacer()
                                PreTransactionBuyCell(store: item.store, share: item.share)
                                    .frame(width: 220, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            }
                            .onTapGesture {
                                
                                let urlString = "amihexin://command=gotopage&pageid=0001"
                                if let url = URL(string: urlString) {
                                    
                                    UIApplication.shared.open(url, options: [:],
                                                              completionHandler: {
                                                                (success) in
                                                              })
                                }
                            }
                            
                        }
                    }
                    Section(header: Text("买出")){
                        ForEach(store.outstores) { item in
                            HStack{
                                ShareView(share: item.share)
                                Spacer()
                                PreTransactionSellerCell(store: item.store, share: item.share)
                                    .frame(width: 220, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                    
                            }
                            .onTapGesture {
                                let urlString = "amihexin://"
                                if let url = URL(string: urlString) {
                                    //根据iOS系统版本，分别处理
                                    if #available(iOS 10, *) {
                                        UIApplication.shared.open(url, options: [:],
                                                                  completionHandler: {
                                                                    (success) in
                                                                  })
                                    } else {
                                        UIApplication.shared.openURL(url)
                                    }
                                }
                            }
                            
                        }
                    }
                }
            }
            
            .alert(item: $store.alertData){ alertData in
                Alert(
                    title: Text(alertData.title),
                    message: Text(alertData.message)
                        
                )
            }
            
           
            .navigationBarTitle(Text("预交易"))
        }
        .onAppear(){
            store.update()
        }
    }
}

struct PreTransactionPage_Previews: PreviewProvider {
    static var previews: some View {
        PreTransactionPage()
    }
}
